class TreeNode(object):
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
class Solution(object):
    def createBinaryTree(self, descriptions):
        d = {}
        ch_list = set()
        for p, ch, state in descriptions:
            if p not in d:
                p_node = TreeNode(p)
                d[p] = p_node
            else:
                p_node = d[p]
            if ch not in d:
                ch_node = TreeNode(ch)
                d[ch] = ch_node
            else:
                ch_node = d[ch]
            if state == 1:
                p_node.left = ch_node
            else:
                p_node.right = ch_node
            ch_list.add(ch)
        for key in d:
            if key not in ch_list:
                return d[key]


